package com.houxy.controller;

import com.houxy.repository.model.bo.RegisterBO;
import com.houxy.repository.model.entity.Register;
import com.houxy.repository.model.entity.User;
import com.houxy.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Hou
 * @date 2019/3/19 10:31
 * @describe：
 */
@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @RequestMapping("/addRegister")
    public ModelAndView addRegister(Register register, String time, HttpSession session) {
        register.setCreateTime(register.getCreateTime() + " " + time);
        ModelAndView modelAndView = new ModelAndView();
        boolean flag = false;
        User currentUser = (User) session.getAttribute("currentUser");
        register.setUserId(currentUser.getId());
        List<RegisterBO> registerBOList = registerService.findRegisterByUser(currentUser.getId());
        if (registerBOList != null) {
            for (RegisterBO registerBO : registerBOList) {
                if ((register.getExpertId().equals(registerBO.getExpertId()))
                        && (register.getCreateTime().equals(registerBO.getCreateTime()))) {
                    flag = false;
                    break;
                } else {
                    flag = true;
                }
            }
        } else {
            flag = true;
        }
        if (flag == true) {

            flag = registerService.addRegister(register);
            if (flag == true) {
                modelAndView.addObject("addRegisterInfo", "成功挂号");

            } else {
                modelAndView.addObject("addRegisterInfo", "挂号失败！当前医生这个时间段人数已满！");
            }
        } else {
            modelAndView.addObject("addRegisterInfo", "挂号失败！一个医生当天只能挂一次号！");
        }
        modelAndView.setViewName("front/register/regsiterReuslt");
        return modelAndView;
    }

    @RequestMapping("/deleteRegiser")
    public String deleteRegister(int id, HttpSession session) {

        boolean deleteRegiset = registerService.deleteRegiset(id);
        if (deleteRegiset == true) {
            session.setAttribute("result", "取消挂号成功！");
            return "common/msg";
        }
        session.setAttribute("result", "取消挂号失败，请联系管理员！");
        return "common/msg";
    }

    @RequestMapping("/confirm")
    public String confirm(Register register, HttpSession session) {
        registerService.update(register.getId());
        session.setAttribute("result", "就医成功");
        return "common/msg";
    }
}
